package com.briup.day18;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author ThinkBook
 */
public class Test03_Iterator {
    //迭代器遍历集合
    public static void main(String[] args) {
        Collection<String> coll = new ArrayList<>();
        coll.add("hello");
        coll.add("hello");
        coll.add("123");
        coll.add("hello");
        //迭代器遍历
        //1.获取迭代器对象
        Iterator<String> iterator = coll.iterator();
        //2.借助while循环  hasNext() next() 实现遍历
        while (iterator.hasNext()){
            String str = iterator.next();
            System.out.print(str + "\t");
            //遍历集合中要避免修改集合
//            if ("123".equals(str)){
//                coll.add("456");
//            }  error报错
        }
        System.out.println();
    }
}
